package com.yikaiye.android.yikaiye.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yikaiye.android.yikaiye.R;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerView通用适配器
 */
public abstract class RecyclerCommonAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private int mLayoutId;
    private List<T> mData;
    private OnItemClickListener listener;
    private RecyclerView recyclerView;

    public RecyclerCommonAdapter(Context context, int layoutId, List<T> data) {
        mContext = context;
        mLayoutId = layoutId;
        if (mData == null){
            mData = new ArrayList<>();
        }
        this.mData.clear();
        this.mData.addAll(data);
    }

    public void onRefresh(List<T> mList) {
        if (mList != null) {
            if (mData == null){
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    public void addData(List<T> mList) {
        if (mList != null && mList.size() != 0) {
            if (mData == null){
                mData = new ArrayList<>();
            }
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }
    public List<T> getmData(){
        return this.mData;
    }

    /**
     * 在RecyclerView提供数据的时候调用
     */
    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    /**
     * 定义一个点击事件接口回调
     */
    public interface OnItemClickListener {
        /**
         * 条目点击事件
         *
         * @param parent   RecyclerView
         * @param view     v
         * @param position 位置
         */
        void onItemClick(RecyclerView parent, View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        return RecyclerViewHolder.get(mContext, parent, mLayoutId);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof RecyclerViewHolder) {
            RecyclerViewHolder holder = (RecyclerViewHolder) viewHolder;
            bindClick(holder, mData.get(holder.getLayoutPosition()), position);
            convert(holder, mData.get(position), position);
            //条目点击事件
            holder.itemView.setOnClickListener(view -> {

                if (listener != null && view != null && recyclerView != null) {
                    int position1 = recyclerView.getChildAdapterPosition(view);
                    listener.onItemClick(recyclerView, view, position1);
                }
            });
        }
    }

    /**
     * 利用抽象方法回传出去，每个不一样的Adapter去设置
     *
     * @param holder   holder对象
     * @param item     实体类
     * @param position 位置
     */
    public abstract void convert(RecyclerViewHolder holder, T item, int position);

    /**
     * 条目中子控件点击事件
     *
     * @param holder holder对象
     * @param item   实体类
     */
    public void bindClick(RecyclerViewHolder holder, T item, int position) {

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}
